using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Web;

namespace ProjectCodeS.Models
{ 
    public class ServiceTypeRepository : IServiceTypeRepository
    {
        ProjectCodeSContext context = new ProjectCodeSContext();

        public IQueryable<ServiceType> All
        {
            get { return context.ServiceTypes; }
        }

        public IQueryable<ServiceType> AllIncluding(params Expression<Func<ServiceType, object>>[] includeProperties)
        {
            IQueryable<ServiceType> query = context.ServiceTypes;
            foreach (var includeProperty in includeProperties) {
                query = query.Include(includeProperty);
            }
            return query;
        }

        public ServiceType Find(int id)
        {
            return context.ServiceTypes.Find(id);
        }

        public void InsertOrUpdate(ServiceType servicetype)
        {
            if (servicetype.Id == default(int)) {
                // New entity
                context.ServiceTypes.Add(servicetype);
            } else {
                // Existing entity
                context.Entry(servicetype).State = EntityState.Modified;
            }
        }

        public void Delete(int id)
        {
            var servicetype = context.ServiceTypes.Find(id);
            context.ServiceTypes.Remove(servicetype);
        }

        public void Save()
        {
            context.SaveChanges();
        }

        public void Dispose() 
        {
            context.Dispose();
        }
    }

    public interface IServiceTypeRepository : IDisposable
    {
        IQueryable<ServiceType> All { get; }
        IQueryable<ServiceType> AllIncluding(params Expression<Func<ServiceType, object>>[] includeProperties);
        ServiceType Find(int id);
        void InsertOrUpdate(ServiceType servicetype);
        void Delete(int id);
        void Save();
    }
}